package cancer

import (
	"encoding/json"
	"gitcode.com/G-YT/iHealth25/server/global"
	"gitcode.com/G-YT/iHealth25/server/model/cancer"
	"gitcode.com/G-YT/iHealth25/server/model/common/response"
	"gitcode.com/G-YT/iHealth25/server/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type CancerApi struct {
}

// GetCancerQuestions 获取癌症问题列表
// @Tags Cancer
// @Summary 获取癌症问题列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query cancer.question true "获取癌症问题列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /api/cancer/getCancerQuestions [get]
func (cancerApi *CancerApi) GetCancerQuestions(c *gin.Context) {
	data, _ := CancerService.GetCancerQuestions()
	response.Result(200, data, "获取成功!", c)
}

// CreateCancerOptions 创建癌症选项集
// @Tags Cancer
// @Summary 创建癌症选项集
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query cancer.options true "创建癌症选项集"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
// @Router /api/cancer/createCancerOptions [post]
func (cancerApi *CancerApi) CreateCancerOptions(c *gin.Context) {
	var co cancer.Options
	err := c.ShouldBindJSON(&co)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	verify := utils.Rules{
		"options":   {utils.NotEmpty()},
		"is_infect": {utils.NotEmpty()},
		"is_inject": {utils.NotEmpty()},
	}
	if err = utils.Verify(co, verify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	var info cancer.Options
	info.UID = utils.GetUserID(c)
	info.Options, _ = json.Marshal(co.Options)
	info.IsInject = co.IsInject
	info.IsInfect = co.IsInfect
	optionsInfo, _ := CancerService.GetCancerOptionsByUid(info.UID)
	// 判断是否存在用户功效标签
	if optionsInfo.ID != 0 {
		if err = CancerService.UpdateCancerOptions(optionsInfo.ID, &info); err != nil {
			global.GVA_LOG.Error("保存失败!", zap.Error(err))
			response.FailWithMessage("保存失败", c)
		}
	} else {
		if err = CancerService.CreateCancerOptions(&info); err != nil {
			global.GVA_LOG.Error("保存失败!", zap.Error(err))
			response.FailWithMessage("保存失败", c)
		}
	}
	response.Result(200, "", "保存成功", c)
}

// GetScreenReport 获取筛查报告
// @Tags Cancer
// @Summary 获取筛查报告
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query cancer.User true "获取筛查报告"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /api/cancer/getScreenReport [get]
func (cancerApi *CancerApi) GetScreenReport(c *gin.Context) {
	uid := utils.GetUserID(c)
	data, err := CancerService.GetScreenReport(uid)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.Result(200, data, "获取成功", c)
	}
}
